﻿namespace REVISIONMAX20
{
    using System;
    using System.Data.SQLite;
    using System.Text;

    public class Source : ObjectBase
    {
        private bool _recursive;
        private string _regex;
        private string _source;

        public Source(SQLiteDataReader reader, ObjectBase owner) : base("sources", reader, owner)
        {
            this._source = reader.GetValue(4).StringValueOrEmpty();
            this._regex = reader.GetValue(5).StringValueOrEmpty();
            this._recursive = reader.GetValue(6).BoolValue();
        }

        public Source(string psource, ObjectBase owner, SQLiteConnection con) : base("sources", owner, con)
        {
            this._source = psource;
        }

        public StringBuilder SaveOrUpdate()
        {
            StringBuilder errors = new StringBuilder();
            try
            {
                using (SQLiteConnection connection = Common.GetSQLiteConnection())
                {
                    connection.Open();
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
                    {
                        using (SQLiteCommand command = connection.CreateSourceCommand())
                        {
                            this.SaveOrUpdate(command, errors);
                        }
                        if (errors.Length == 0)
                        {
                            transaction.Commit();
                            return errors;
                        }
                        transaction.Rollback();
                        return errors;
                    }
                }
            }
            catch (Exception exception)
            {
                base.Rollback();
                errors.AppendLine(string.Format("Error: {0}", exception.Message));
            }
            return errors;
        }

        public void SaveOrUpdate(SQLiteCommand command, StringBuilder errors)
        {
            if (base.Status != Status.StatusPopulated)
            {
                try
                {
                    base.UpdateCommand(command);
                    command.Parameters["@source"].Value = this._source;
                    command.Parameters["@regex"].Value = this._regex;
                    command.Parameters["@recursive"].Value = this._recursive;
                    if (command.ExecuteNonQuery() <= 0)
                    {
                        errors.AppendLine(string.Format("Error: {0} {1} {2} failed.", base.tablename, base.id, (base.Status == Status.StatusNew) ? "insert" : "update"));
                    }
                    else
                    {
                        base.Commit();
                        base.Persist();
                    }
                }
                catch (Exception exception)
                {
                    errors.AppendLine(string.Format("Error: {0}", exception.Message));
                }
            }
        }

        public bool recursive
        {
            get
            {
                return this._recursive;
            }
            set
            {
                base.IsDirty = value != this._recursive;
                this._recursive = value;
            }
        }

        public string regex
        {
            get
            {
                return this._regex;
            }
            set
            {
                base.IsDirty = value != this._regex;
                this._regex = value;
            }
        }

        public string source
        {
            get
            {
                return this._source;
            }
            set
            {
                base.IsDirty = value != this._source;
                this._source = value;
            }
        }
    }
}

